/**
 * Copyright (C) 2013-2015
 *
 * @file   on_property_get.c
 *
 * @remark
 *
 */

#include "on_property_set.h"
#include "iid.h"
#include "S_2_Curtain_doSet.h"
#include "S_3_Function_doSet.h"

static void P_128_1_GMT_Offset_doSet(property_operation_t *o)
{
    // 判断数据格式是否正确，如果错误，返回代码: OPERATION_ERROR_VALUE
    if (o->value->format != PROPERTY_FORMAT_LONG && o->value->format != PROPERTY_FORMAT_NUMBER)
    {
        o->code = OPERATION_ERROR_VALUE;
        return;
    }

    // TODO: 执行写操作: o->value->data.number.value.integerValue;

    // 如果成功，返回代码: OPERATION_OK
    o->code = OPERATION_OK;

    return;
}

static void P_128_2_Weather_doSet(property_operation_t *o)
{
    // 判断数据格式是否正确，如果错误，返回代码: OPERATION_ERROR_VALUE
    if (o->value->format != PROPERTY_FORMAT_LONG && o->value->format != PROPERTY_FORMAT_NUMBER)
    {
        o->code = OPERATION_ERROR_VALUE;
        return;
    }

    // TODO: 执行写操作: o->value->data.number.value.integerValue;

    // 如果成功，返回代码: OPERATION_OK
    o->code = OPERATION_OK;

    return;
}

static void P_128_3_UTC_Time_doSet(property_operation_t *o)
{
    // 判断数据格式是否正确，如果错误，返回代码: OPERATION_ERROR_VALUE
    if (o->value->format != PROPERTY_FORMAT_ULONG && o->value->format != PROPERTY_FORMAT_NUMBER)
    {
        o->code = OPERATION_ERROR_VALUE;
        return;
    }

    // TODO: 执行写操作: o->value->data.number.value.integerValue;

    // 如果成功，返回代码: OPERATION_OK
    o->code = OPERATION_OK;

    return;
}

static void S_128_Server_Info_doSet(property_operation_t *o)
{
    switch (o->piid)
    {
        case IID_128_1_GMT_Offset:
            P_128_1_GMT_Offset_doSet(o);
            break;

        case IID_128_2_Weather:
            P_128_2_Weather_doSet(o);
            break;

        case IID_128_3_UTC_Time:
            P_128_3_UTC_Time_doSet(o);
            break;

        default:
            o->code = OPERATION_ERROR_CANNOT_WRITE;
            break;
    }
}

void on_property_set(property_operation_t *o)
{
    MI_LOG_INFO("[on_property_set]siid %d, piid %d\n", o->siid, o->piid);
    
    if (o->value == NULL)
    {
    	MI_LOG_ERROR("value is NULL\n");
    	return;
    }
    
    switch (o->siid)
    {
        case IID_2_Curtain:
            S_2_Curtain_doSet(o);
            break;

        case IID_3_Function:
            S_3_Function_doSet(o);
            break;

        case IID_128_Server_Info:
            S_128_Server_Info_doSet(o);
            break;

        default:
            o->code = OPERATION_OK;
            break;
    }
}
